#include "custommanager.h"

Vec3Manager::Vec3Manager(QObject* parent /*= nullptr*/)
	:QtAbstractPropertyManager(parent)
{
	m_pDoublePropertyManager = new QtDoublePropertyManager(this);
	connect(m_pDoublePropertyManager, &QtDoublePropertyManager::valueChanged, [=](QtProperty* prop,double val) {
		if (m_mapX2Property.keys().contains(prop))
		{
			auto parentProp = m_mapX2Property[prop];
			m_mapProperty2Value[parentProp].m_nX = val;

			emit valueChanged(parentProp, m_mapProperty2Value[parentProp]);
		}
		if (m_mapY2Property.keys().contains(prop))
		{
			auto parentProp = m_mapY2Property[prop];
			m_mapProperty2Value[parentProp].m_nY = val;

			emit valueChanged(parentProp, m_mapProperty2Value[parentProp]);
		}
		if (m_mapZ2Property.keys().contains(prop))
		{
			auto parentProp = m_mapZ2Property[prop];
			m_mapProperty2Value[parentProp].m_nZ = val;

			emit valueChanged(parentProp, m_mapProperty2Value[parentProp]);
		}
	});
}

Vec3Manager::~Vec3Manager()
{
	clear();
}

QtDoublePropertyManager* Vec3Manager::subDoublePropertyManager() const
{
	return m_pDoublePropertyManager;
}

void Vec3Manager::initializeProperty(QtProperty* property)
{
	Vec3 vec = { 0,0,0};
	m_mapProperty2Value[property] = vec;

	QtProperty* xProp = m_pDoublePropertyManager->addProperty();
	xProp->setPropertyName("X");
	m_pDoublePropertyManager->setValue(xProp, vec.m_nX);
	m_mapProperty2X[property] = xProp;
	m_mapX2Property[xProp] = property;
	property->addSubProperty(xProp);

	QtProperty* yProp = m_pDoublePropertyManager->addProperty();
	yProp->setPropertyName("Y");
	m_pDoublePropertyManager->setValue(yProp, vec.m_nY);
	m_mapProperty2Y[property] = yProp;
	m_mapY2Property[yProp] = property;
	property->addSubProperty(yProp);

	QtProperty* zProp = m_pDoublePropertyManager->addProperty();
	zProp->setPropertyName("Z");
	m_pDoublePropertyManager->setValue(zProp, vec.m_nZ);
	m_mapProperty2Z[property] = zProp;
	m_mapZ2Property[zProp] = property;
	property->addSubProperty(zProp);
}

void Vec3Manager::uninitializeProperty(QtProperty* property)
{
	QtProperty* Prop = m_mapProperty2X[property];
	if (Prop) 
	{
		m_mapX2Property.remove(Prop);
		delete Prop;
	}
	m_mapX2Property.remove(property);

	Prop = m_mapProperty2Y[property];
	if (Prop)
	{
		m_mapY2Property.remove(Prop);
		delete Prop;
	}
	m_mapY2Property.remove(property);

	Prop = m_mapProperty2Z[property];
	if (Prop)
	{
		m_mapZ2Property.remove(Prop);
		delete Prop;
	}
	m_mapZ2Property.remove(property);
	
	m_mapProperty2Value.remove(property);
}

void Vec3Manager::setValue(QtProperty* property, const Vec3& val)
{
	auto it = m_mapProperty2Value.find(property);
	if (it == m_mapProperty2Value.end())
		return;

	Vec3 oldVal = it.value();
	if (oldVal == val)
		return;

	it.value() = val;

	m_pDoublePropertyManager->setValue(m_mapProperty2X[property], val.m_nX);
	m_pDoublePropertyManager->setValue(m_mapProperty2Y[property], val.m_nY);
	m_pDoublePropertyManager->setValue(m_mapProperty2Z[property], val.m_nZ);

	emit propertyChanged(property);
	emit valueChanged(property, val);
}

void Vec3Manager::setMinimum(QtProperty* property, const Vec3& val)
{
	m_pDoublePropertyManager->setMinimum(m_mapProperty2X[property], val.m_nX);
	m_pDoublePropertyManager->setMinimum(m_mapProperty2Y[property], val.m_nY);
	m_pDoublePropertyManager->setMinimum(m_mapProperty2Z[property],val.m_nZ);
}

void Vec3Manager::setMaximum(QtProperty* property, const Vec3& val)
{
	m_pDoublePropertyManager->setMaximum(m_mapProperty2X[property], val.m_nX);
	m_pDoublePropertyManager->setMaximum(m_mapProperty2Y[property], val.m_nY);
	m_pDoublePropertyManager->setMaximum(m_mapProperty2Z[property], val.m_nZ);
}

void Vec3Manager::setSingleStep(QtProperty* property, const Vec3& val)
{
	m_pDoublePropertyManager->setSingleStep(m_mapProperty2X[property], val.m_nX);
	m_pDoublePropertyManager->setSingleStep(m_mapProperty2Y[property], val.m_nY);
	m_pDoublePropertyManager->setSingleStep(m_mapProperty2Z[property], val.m_nZ);
}
